"""
Парсер курса доллара за указанное кол-во лет с ЦБ.
"""
import time

def lst():
	bar = []
	try:
			res = open("usd-rus.html", "r", encoding='utf-8').read()#get("https://www.cbr.ru/currency_base/dynamics/?UniDbQuery.Posted=True&UniDbQuery.so=1&UniDbQuery.mode=1&UniDbQuery.date_req1=&UniDbQuery.date_req2=&UniDbQuery.VAL_NM_RQ=R01235&UniDbQuery.From="+time.strftime("%d.%m.")+str(int(time.strftime("%Y"))-1)+"&UniDbQuery.To="+time.strftime("%d.%m.%Y"), timeout=3)
			#open("html.html", "w", encoding='utf-8').write(res)
			mins = 0#len(res)-1
			for i in range(len(res)):
				try:krs = res[mins:].index("<td>")
				except:continue
				apd = res[mins:][krs+51:krs+58]
				try:zapataia=apd.index(',')
				except:continue
				apd = apd[:zapataia] + '.' + apd[zapataia+1:len(apd)]
				bar.append(float(apd))
				mins = krs+50+mins
				#print(bar)
	except Exception as e: print('[!ERROR!][list_usd_rus]',e)
	bar.reverse()
	return bar

def lst_with_date():
	bar = []
	try:
			res = open("usd-rus.html", "r", encoding='utf-8').read()#get("https://www.cbr.ru/currency_base/dynamics/?UniDbQuery.Posted=True&UniDbQuery.so=1&UniDbQuery.mode=1&UniDbQuery.date_req1=&UniDbQuery.date_req2=&UniDbQuery.VAL_NM_RQ=R01235&UniDbQuery.From="+time.strftime("%d.%m.")+str(int(time.strftime("%Y"))-1)+"&UniDbQuery.To="+time.strftime("%d.%m.%Y"), timeout=3)
			#open("html.html", "w", encoding='utf-8').write(res)
			mins = 0#len(res)-1
			for i in range(len(res)):
				try:krs = res[mins:].index("<td>")
				except:continue
				apd = res[mins:][krs+51:krs+58]
				date = res[mins:][krs+4:krs+14]
				try:zapataia=apd.index(',')
				except:continue
				apd = apd[:zapataia] + '.' + apd[zapataia+1:len(apd)]
				
				bar.append([float(apd), date])
				mins = krs+50+mins
				#print(bar)
	except Exception as e: print('[!ERROR!][list_usd_rus]',e)
	bar.reverse()
	return bar

def lst_only_date():
	bar = []
	try:
			res = open("usd-rus.html", "r", encoding='utf-8').read()#get("https://www.cbr.ru/currency_base/dynamics/?UniDbQuery.Posted=True&UniDbQuery.so=1&UniDbQuery.mode=1&UniDbQuery.date_req1=&UniDbQuery.date_req2=&UniDbQuery.VAL_NM_RQ=R01235&UniDbQuery.From="+time.strftime("%d.%m.")+str(int(time.strftime("%Y"))-1)+"&UniDbQuery.To="+time.strftime("%d.%m.%Y"), timeout=3)
			#open("html.html", "w", encoding='utf-8').write(res)
			mins = 0#len(res)-1
			for i in range(len(res)):
				try:krs = res[mins:].index("<td>")
				except:continue
				date = res[mins:][krs+4:krs+14]
				bar.append(date)
				mins = krs+50+mins
				#print(bar)
	except Exception as e: print('[!ERROR!][list_usd_rus]',e)
	bar.reverse()
	return bar

def new_data(years_old=1):
	from requests import get
	res = get("https://www.cbr.ru/currency_base/dynamics/?UniDbQuery.Posted=True&UniDbQuery.so=1&UniDbQuery.mode=1&UniDbQuery.date_req1=&UniDbQuery.date_req2=&UniDbQuery.VAL_NM_RQ=R01235&UniDbQuery.From="+time.strftime("%d.%m.")+str(int(time.strftime("%Y"))-years_old)+"&UniDbQuery.To="+time.strftime("%d.%m.%Y"), timeout=3)
	open("usd-rus.html", "w", encoding='utf-8').write(res.text)
	
#new_data(9)
